package com.lp.common.dto;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@Service("pageBase")
public class PageBase<T> implements PageInterface<T> {

    private int                 count;


    private int                 pageSize=10;

    private int                 currentPage  = 1;

    private List<T>             data         = new ArrayList<T>();

    private Map<String, Object> variableData = new HashMap<String, Object>();
    /**
     * 数据
     * @param data
     */
    public Map<String, Object> getVariableData() {
        return variableData;
    }
    /**
     * 数据
     * @param data
     */
    public void setVariableData(Map<String, Object> variableData) {
        this.variableData = variableData;
    }
    /**
     * 总条数
     * @param count
     */
    public void setCount(int count) {
        if (count < 0)
            count = 0;
        this.count = count;
    }
    /**
     * 设置分页大小
     * @param pageSize
     */
    public void setPageSize(int pageSize) {
        if (pageSize <= 0)
            pageSize = 1;
        this.pageSize = pageSize;
    }
    /**
     * 设置开始位置
     * @param index
     */
    public void setStartIndex(int index) {
        if (index < 0)
            index = 0;
        if (index == 0)
            currentPage = 1;
        else {
            this.currentPage = index / this.pageSize + 1;
        }
    }
    /**
     * 设置当前页数
     * @param currentPage
     */
    public void setCurrentPage(int currentPage) {
        if (currentPage <= 0)
        { this.currentPage = 1;}else{
        this.currentPage = currentPage;}
    }
    /**
     * 数据
     * @param data
     */
    public void setData(List<T> data) {
        if (data == null)
            return;
        this.data = data;
    }
    /**
     * 获取数据
     * @param data
     */
    public List<T> getData() {
        return this.data;
    }
    /**
     * 获取分页大小
     * @param pageSize
     */
    public int getPageSize() {
        return this.pageSize;
    }
    /**
     * 下一个开始位置
     * @return
     */
    public int getNextStartIndex() {
        int nextStartIndex = currentPage * pageSize + 1;

        //		int nextStartIndex = getEndIndex() + 1;

        if (nextStartIndex > count)
            nextStartIndex = -1;
        return nextStartIndex;
    }
    /**
     * 得到上一个开始位置
     * @return
     */
    public int getPreviousStartIndex() {
        int index = (getCurrentPage() - 2) * pageSize;
        if (index < 0)
            index = 0;
        return index;
    }
    /**
     * 得到最后一条数据的索引
     * @return
     */
    public int getEndIndex() {

        return getStartIndex() + this.pageSize;
    }
    /**
     * 得到下一个页数
     * @return
     */
    public int getNextPage() {
        if (getPageTotal() == getCurrentPage())
            return getCurrentPage();
        return getCurrentPage() + 1;
    }
    /**
     * 得到上一页页数
     * @return
     */
    public int getPreviousPage() {
        if (getCurrentPage() == 1)
            return getCurrentPage();
        return getCurrentPage() - 1;
    }
    /**
     * 开始位置
     * @return
     */
    public int getStartIndex() {
        int startIndex = (this.currentPage - 1) * this.pageSize;
        return startIndex;
    }
    /**
     * 得到总页数
     * @return
     */
    public int getPageTotal() {
        int pageTotal = this.getCount() / this.getPageSize();
        if (this.getCount() == 0)
            pageTotal = 1;
        else if (getCount() % this.getPageSize() > 0)
            pageTotal++;
        return pageTotal;
    }
    /**
     * 是否结束1.已结束0.未结束
     * @return
     */
    public byte getEnd() {
        byte flag = 0;
        if (getPageTotal() <= this.getCurrentPage()) {
            flag = 1;
        }
        return flag;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public int getCount() {
        return this.count;
    }

    public int getDataCount() {
        int dataCount = 0;
        if (data != null)
            dataCount = data.size();
        return dataCount;
    }
    /**
     * 开始位置
     * @return
     */
    public void setStartIndex(String startindex) {
        try {
            int start = Integer.parseInt(startindex);
            setStartIndex(start);
        }
        catch (NumberFormatException e1) {
            setStartIndex(0);
        }
    }

}
